import execjs
import json
import requests

headers = {
    "accept": "application/json, text/plain, */*",
    "accept-language": "zh-CN,zh;q=0.9",
    "content-type": "application/json;charset=UTF-8",
    "origin": "https://www.xiaohongshu.com",
    "priority": "u=1, i",
    "referer": "https://www.xiaohongshu.com/",
    "sec-ch-ua": "\"Google Chrome\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
    "x-b3-traceid": "d9589f56e7e00940",
    "x-mns": "unload",
    # "x-s": "XYW_eyJzaWduU3ZuIjoiNTYiLCJzaWduVHlwZSI6IngyIiwiYXBwSWQiOiJ4aHMtcGMtd2ViIiwic2lnblZlcnNpb24iOiIxIiwicGF5bG9hZCI6IjgyYmNkNjIwMzdiNzBiNWYwNzg3ODQ5YjZhZWQxMTI0OWIwNDJjMzMxZTEwZTNmMjdlZTRmMjlhYTBmOGMwOTg4MjQ4MWIzM2Y5ZDJlYWExOTIzZTgwMjczZTRkNzFkMDU5Y2VjNmI0YTg2ZmI5NTY1ZmE4MjdkN2Q3OGZkN2E5NjkwNGQxNmYwNWMwMDEzMjBlOGY2NTVjNzRmNmJmMDNlNmIxY2NlNTliNDg2NzVhYjFlMzVhYTgwNWE0OWIwODBiYjUyNjI1ZGVhMWZlOWE4ZjNiYTg5YzEwM2EwODhkYzk1MWVhZDkyZjQ1ZjcxMGVhOGRhNjRlYzViNDhiNTM4MDE1NWYxZGFhMDRkMjJkY2FiMWFjMjRkMzZhOWE2MDY1ODRlNjM0YWY1OGI3MzFhMDlkMDU0MDJmODUwYjQ5YWUwNDQ0YTc3ODZmYjU2NjQ1NDZmZThkODI5MDRmNzA2OGQ3MGIzYzhiMTV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4dp3agY0qDESLnYOqDESygp7aDShz7QQyo8A8bmFqokVagm0NFkAzrQ8PFSkGFIU4gzNnSm7yrS9P9pnLoz3/FMN8/+QcnLl/b+6aLptq9Ts/7+L4gzcGMmFcDS9J9pxpd4kanD78nTA4gSQcFTA8B898Lzn4b+Q2B4A2opFJg+CzbzQPMbQanVI8pz0+7+h8/4APBP78gYn47SQzpz3agYkaaHVHdWEH0iU+/HF+ADIP0LVHdWlPsHCP0HVHdWlPaHCHfE6qfMYJsQR",
    # "x-t": "1751448319058",
    "x-xray-traceid": "cbe53a3a002ffbc983b34075d4ee2821"
}
cookies = {
    "a1": "1960e2acef4ev020adbqbgxdcps88phbukzabnajf50000272280",
    "webId": "4b6833e542f84e76fbfb34e006eddce8",
    "gid": "yjK8dJ0fS47WyjK8dJ0SdTI0i4dh8J80fDADuVikCfSUM028YYUxDD888JWJJY88jy8DJjDy",
    "customerClientId": "279177063808841",
    "abRequestId": "4b6833e542f84e76fbfb34e006eddce8",
    "x-user-id-fankui-out.xiaohongshu.com": "616fdd2b85d0b300016960dc",
    "x-user-id-partner.xiaohongshu.com": "616fdd2b85d0b300016960dc",
    "x-user-id-pgy.xiaohongshu.com": "64faeddd7500000000000010",
    "x-user-id-ad-market.xiaohongshu.com": "616fdd2b85d0b300016960dc",
    "access-token-ad-market.xiaohongshu.com": "customer.ad_market.AT-68c517519459434552924512enefn093ihp1uxmh",
    "access-token-chengfeng.xiaohongshu.com": "customer.ad_wind.AT-68c517520129449450949557pczftjoif2nx0web",
    "webBuild": "4.70.2",
    "web_session": "040069b47b667a0c594b13eb563a4bf1a8fe62",
    "access-token-ad.xiaohongshu.com": "customer.leona.AT-68c517522291385664194259wxwdatcmj0nalkml",
    "customer-sso-sid": "68c517522294611186635807orsfjexrzd91iu2r",
    "ads-partner-sid": "4654b314944a4dbcbc9dfc7887244a5d",
    "system.alias": "ads_partner",
    "access-token-partner.xiaohongshu.com": "customer.ads_partner.AT-68c517522294611184654297yuaz0twyfmji5h0m",
    "ares.beaker.session.id": "1751444908302086821763",
    "acw_tc": "0a4ae10f17514500051245890e6db398f101a43ed3cfc8c72a8f1346e05c2b",
    "websectiga": "9730ffafd96f2d09dc024760e253af6ab1feb0002827740b95a255ddf6847fc8",
    "sec_poison_id": "ac5cd95b-6885-4ee1-b42e-2db3e727a579",
    "xsecappid": "xhs-pc-web",
    "loadts": "1751451589701",
    "unread": "{%22ub%22:%2268629f8f000000002201f520%22%2C%22ue%22:%226863d4d9000000001c031d87%22%2C%22uc%22:30}"
}


url = "https://edith.xiaohongshu.com/api/sns/web/v1/feed"
data = {
    "source_note_id": "685b75bd0000000010011b27",
    "image_formats": [
        "jpg",
        "webp",
        "avif"
    ],
    "extra": {
        "need_body_topic": "1"
    },
    "xsec_source": "pc_feed",
    "xsec_token": "ABLNnY8yjzBTPEOktZ6hQZDmdmFRgBvxEagF0OL3t7VD4="
}

get_xs_headers = execjs.compile(open('./xhs.js', 'r', encoding='utf-8').read()).call("get_headers", '/api/sns/web/v1/feed', data)

print(len(get_xs_headers['X-s']))

headers['x-s'] = get_xs_headers['X-s']
headers['x-t'] = str(get_xs_headers['X-t'])

data = json.dumps(data, separators=(',', ':'))
response = requests.post(url, headers=headers, cookies=cookies, data=data)

print(response.text)
